package com.baidu.controller;


import com.baidu.constant.MessageConstant;
import com.baidu.constant.RedisMessageConstant;
import com.baidu.entity.Result;
import com.baidu.utils.SMSUtils;
import com.baidu.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

	@Autowired
	private JedisPool jedisPool;

	@RequestMapping("/orderCode")
	public Result SendOrderCode(String telephone){

		try {
			String orderCode = ValidateCodeUtils.generateValidateCode4String(4);
			SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,orderCode);//发送给用户的手机
			jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,30000000,orderCode);


		return	new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,orderCode);//发送到后台

		} catch (Exception e) {
		return	new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
		}


	}

	@RequestMapping("/send4Login")
	public Result SendOrderLoginCode(String telephone){

		try {
			String orderCode = ValidateCodeUtils.generateValidateCode4String(6);
			SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,orderCode);//发送给用户的手机
			jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,30000000,orderCode);


			return	new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,orderCode);//发送到后台

		} catch (Exception e) {
			return	new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
		}


	}



}
